package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.mapper.AgreeMapper;
import com.example.demo.mapper.ArticleMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.entity.Agree;
import com.example.demo.model.entity.Article;
import com.example.demo.model.entity.User;
import com.example.demo.model.vo.ArticleVo;
import com.example.demo.service.AgreeService;
import com.example.demo.service.ArticleService;
import com.example.demo.utils.DataTimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class AgreeServiceImpl implements AgreeService {

    @Resource
    private AgreeMapper agreeMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public Agree agree(Agree agree) {
        QueryWrapper<Agree> wrapper = new QueryWrapper<>();
        wrapper.in("id",agree.getId());
        wrapper.in("uid",agree.getUid());
        if(assembly(wrapper)){
            agree.setCreateAt(DataTimeUtil.getNowTimeString());
            agreeMapper.insert(agree);
            return agree;
        }
        else{
            return agree;
        }

    }

    @Override
    public void disagree(Agree agree) {
        Map<String, Object> columnMap = new HashMap<String, Object>();
        columnMap.put("id", agree.getId());
        columnMap.put("uid",agree.getUid());
        agreeMapper.deleteByMap(columnMap);
    }

    @Override
    public Boolean findById(Agree agree) {
        QueryWrapper<Agree> wrapper = new QueryWrapper<>();
        wrapper.in("id",agree.getId());
        wrapper.in("uid",agree.getUid());
        if(assembly(wrapper)){
            return false;
        }
        return true;
    }

    private Boolean assembly(QueryWrapper<Agree> wrapper) {
        boolean result = true;
        List<Agree> agree = agreeMapper.selectList(wrapper);
        System.out.println(agree);
        if (agree.size()!=0){
            result = false;
        }
        return result;
    }
}
